package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.BitmapDetail;
import com.hpin.mlxteacher.message.EditMessageActivity;
import com.hpin.mlxteacher.tree.ReleaseActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;

import java.util.List;

/**
 * Created by Aaron on 2015/7/22.
 */
public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    private List<BitmapDetail> bitmapsDetail;
    private List<String> pics;


    public ImageAdapter(Context ctx, List<BitmapDetail> bitmapsDetail,List<String> pics) {
        this.ctx = ctx;
        this.bitmapsDetail = bitmapsDetail;
        this.pics = pics;
    }

    public void setBitmapsDetail(List<BitmapDetail> bitmapsDetail,List<String> pics) {
        this.bitmapsDetail = bitmapsDetail;
        this.pics = pics;
    }

    @Override
    public int getCount() {
        return bitmapsDetail.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_image, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.iv_delete.setVisibility(View.INVISIBLE);

        if (Constant.ADDBITMAPPATH.equals(bitmapsDetail.get(position).mPath)) {

            viewHolder.iv_image.setImageResource(R.drawable.icon_add);
            viewHolder.iv_delete.setVisibility(View.INVISIBLE);

        } else {
            viewHolder.iv_image.setImageBitmap(CommonUtils.getBitmapFromPath(bitmapsDetail.get(position).mPath));
            viewHolder.iv_delete.setVisibility(View.VISIBLE);
        }

        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除照片
                String url = bitmapsDetail.get(position).mPath;
                bitmapsDetail.remove(position);
                pics.remove(url);

                //照片选择的页面 集合中的图片也删除掉
                MyAdapter.mSelectedImage.remove(url);
                //编辑页面的 图片集合 也删除掉
                if (ctx instanceof EditMessageActivity) {

                    EditMessageActivity editMessageActivity = (EditMessageActivity) ctx;
                    List<BitmapDetail> loadedPics = editMessageActivity.loadedPics;
                    loadedPics.remove(position);

                    if (loadedPics.size() == 8) {

                        bitmapsDetail.add(new BitmapDetail(Constant.ADDBITMAPPATH, Constant.ADDBITMAPPATH));
                    }


                } else if (ctx instanceof ReleaseActivity) {

                    ReleaseActivity editMessageActivity = (ReleaseActivity) ctx;
                    List<BitmapDetail> loadedPics = editMessageActivity.loadedPics;
                    loadedPics.remove(position);

                    if (loadedPics.size() == 8) {

                        bitmapsDetail.add(new BitmapDetail(Constant.ADDBITMAPPATH, Constant.ADDBITMAPPATH));
                    }

                }

//                LogUtil.e("TAG", "移除的图片的地址==>" + "adapter中的::::" + MyAdapter.mSelectedImage.get(position)
//                        + "activity中的======>" + loadedPics.get(position).mPath);


                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_image;
        private ImageView iv_delete;
    }


}
